﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Edu.Web.Data.DAO;
using Edu.Web.Data.Entity;
using Edu.Web.Security;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Data.Service.Impl
{
    public class AccountService : IAccountService
    {
        private IUserDAO _userDao;
        private ITeacherDAO _teacherDao;

        private IStudentDAO _studentDao;

        public AccountService(IUserDAO userDao, ITeacherDAO teacherDao, IStudentDAO studentDao)
        {
            _userDao = userDao;
            _teacherDao = teacherDao;
            _studentDao = studentDao;
        }


        public User SignIn(string userName, string userPwd)
        {

            UserEntity userEntity = _userDao
                .LoadEntities(x => x.UserName.Equals(userName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

            if (userEntity == null)
            {
                throw new Exception("不存在该用户！");
            }
            if (!userEntity.UserPassword.Equals(userPwd))
            {
                throw new Exception("密码输入错误！");
            }

            var user = new User { RealName = userEntity.RealName, UserId = userEntity.Id, UserName = userEntity.UserName, UserType = userEntity.UserType ?? 3 };

            if (userEntity.UserType.Equals(2))
            {
                var techer = _teacherDao.LoadEntities(x => x.UserId == userEntity.Id).FirstOrDefault();
                if (techer.IsNotNull())
                {
                    user.TeacherType = techer.TeacherType;
                    // user.TeacherId = techer.Id;
                }
            }
            else
            {
                //var student = _studentDao.LoadEntities(x => x.UserId == userEntity.Id).FirstOrDefault();
                //if (student.IsNotNull())
                //{
                //    user.StudentId = student.Id;
                //}
            }

            return user;

        }
    }
}